package com.lanhai.crowd.util;

/**
 * 同意AJAX请求中的返回结果
 * @param <T>
 */
public class ResultEntity<T> {

    private static final String SUCCESS = "SUCCESS";
    private static final String ERROR = "ERROR";

    public String state;
    public String message;
    public T data;

    /**
     * 请求成功且不需要返回数据
     *
     * @param <E>
     * @return
     */
    public static <E> ResultEntity<E> ok() {
        return new ResultEntity<>(SUCCESS, null, null);
    }

    /**
     * 请求成功且需要返回数据
     * @param data
     * @param <E>
     * @return
     */
    public static <E> ResultEntity<E> data(E data) {
        return new ResultEntity<>(SUCCESS, null, data);
    }

    /**
     * 请求失败且返回失败信息
     * @param msg
     * @param <E>
     * @return
     */
    public static <E> ResultEntity<E> error(String msg){
        return new ResultEntity<>(ERROR, msg, null);
    }

    private ResultEntity() {
    }

    private ResultEntity(String result, String message, T data) {
        this.state = result;
        this.message = message;
        this.data = data;
    }

    @Override
    public String toString() {
        return "ResultEntity{" +
                "state='" + state + '\'' +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
